<template>
  <el-dialog
    v-el-drag-dialog
    title="退单/确认"
    :visible.sync="visible"
    width="800px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" :rules="rules" label-position="right" label-width="100px">
      <el-form-item label="客户名称:">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户电话:">
        <el-input v-model="temp.contactPhone" disabled />
      </el-form-item>
      <el-form-item label="客户地址:" class="block">
        <el-input v-model="temp.contactAddress" disabled />
      </el-form-item>
      <el-form-item class="block" label="退单原因:" prop="marker">
        <el-input v-model="temp.marker" type="textarea" :rows="3" maxlength="200" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryReturnBottlerOrderBack } from '@/api'

  export default {
    name: 'back-order',
    data() {
      return {
        visible: false,
        temp: {
          marker: ''
        },
        rules: {
          marker: [{ required: true, message: '请输入退单原因', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            backCode: this.temp.backCode,
            marker: this.temp.marker
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryReturnBottlerOrderBack(params)
        this.resolve(true)
        this.visible = false
        this.$emit('refresh')
        this.$notify({
          title: '提示',
          message: '退单成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          marker: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
